package cn.kp.learning.controller;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.kp.learning.model.CoreUserPermission;
import cn.kp.learning.model.CoreUserPermissionExample;
import cn.kp.learning.service.CoreUserPermissionService;
import cn.kp.learning.util.ResponseUtil;

@Controller
@RequestMapping("/userPermission")
public class UserPermissionController {

	@Resource
	private CoreUserPermissionService userPermissionService;
	
	@RequestMapping("/addUserPermission")
	public void addUserPermission(HttpServletResponse response,HttpServletRequest request, CoreUserPermission cup) throws Exception{
		System.out.println("roles adding " );
		JSONObject json=new JSONObject();
		cup.setValid(1);
		CoreUserPermissionExample example = new CoreUserPermissionExample();
		//re.createCriteria().andRolesNameEqualTo(rl.getRolesName());
		List<CoreUserPermission> lt = userPermissionService.getPermissionList(example);
		if(lt.size()>0){
			json.put("success", false);
		}else{
			int result = userPermissionService.addUserPermission(cup);
			if(result>0){
				json.put("success", true);
			}else{
				json.put("success", false);
			}
		}		
		try {
			ResponseUtil.write(response, json);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	@RequestMapping("/deleteUserPermission")
	public void deleteUserPermission(HttpServletResponse response,HttpServletRequest request, String itemId) throws Exception{
		System.out.println("roles adding " );
		JSONObject json=new JSONObject();
		int result = userPermissionService.deleteUserPermission(Integer.parseInt(itemId));
		if(result>0){
			json.put("success", true);
		}else{
			json.put("success", false);
		}
		try {
			ResponseUtil.write(response, json);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	@RequestMapping("/getUserPermissionList")
	public void getUserPermissionList(HttpServletResponse response,HttpServletRequest request) throws Exception{
		System.out.println("roles adding " );
		CoreUserPermissionExample example = new CoreUserPermissionExample();
		
		System.out.println("get people list ... ");
		System.out.println("get para : " + request.getParameter("opportunityNO"));
		System.out.println("get para : " + request.getParameter("keyword"));
		System.out.println("get para : " + request.getParameter("search0"));
		System.out.println("get para : " + request.getParameter("search1"));
		System.out.println("get para : " + request.getParameter("search2"));
		System.out.println("get para : " + request.getParameter("search3"));
		System.out.println("get para : " + request.getParameter("search4"));
		System.out.println("get para : " + request.getParameter("search5"));
		System.out.println("get para : " + request.getParameter("limit"));
		System.out.println("get para : " + request.getParameter("start"));
		System.out.println("get para : " + request.getParameter("page"));
		System.out.println("get para : " + request.getParameter("pages"));
		
		String startRows = request.getParameter("start");
		String pagesLimit = request.getParameter("limit");
		if(null!=startRows && !startRows.equals("")){
			example.setStartRow(Integer.parseInt(startRows));
		}
		if(null!=pagesLimit && !pagesLimit.equals("")){
			example.setPageSize(Integer.parseInt(startRows) + Integer.parseInt(pagesLimit));
		}
		int countPermission = userPermissionService.countPermission(example);
		List<CoreUserPermission> lt = userPermissionService.getPermissionList(example);
		
		JSONObject json = new JSONObject();
		JSONArray jsonArray = JSONArray.fromObject(lt);
		json.put("pages", lt.size()/10+1);
		json.put("total", lt.size());
		json.put("rows", jsonArray);
		
		System.out.println("people Json : " + json.toString());
		try {
			ResponseUtil.write(response, json);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	@RequestMapping("/getUserPermission")
	public void getUserPermission(HttpServletResponse response,HttpServletRequest request) throws Exception{
		System.out.println("roles adding " );
		JSONObject json = new JSONObject();
		//HashMap<String,Object> hm = new HashMap<String, Object>();
		
		/*CoreUserPermission coreUserPermission = userPermissionService.getPermission(hm);
		
		JSONArray jsonArray = JSONArray.fromObject(lt);
		json.put("rows", jsonArray);*/
		
		System.out.println("people Json : [waiting for continue...]");
		try {
			ResponseUtil.write(response, json);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
